<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form ref="form" layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="变更编号">
              <a-input placeholder="请输入变更编号" v-model="queryParam.changeCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="专题名称">
              <a-input
                placeholder="请输入专题名称"
                v-model="queryParam.subjectName"
                @click="subjectIdClick"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24" v-show='false'>
            <a-form-item label="专题id">
              <a-input placeholder="请输入专题id" v-model="queryParam.subjectId"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="变更类型">
                <j-dict-select-tag placeholder="请选择变更类型" v-model="queryParam.changeType"
                                   dictCode="hf_project_change_types"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="变更描述">
                <a-input placeholder="请输入变更描述" v-model="queryParam.changeDes"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="变更人">
                <j-select-user-by-dep placeholder="请选择变更人" v-model="queryParam.costWorkday"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="确认人">
                <j-select-user-by-dep placeholder="请选择确认人" v-model="queryParam.confirmorId"/>
                <!--                <a-input placeholder="请输入确认人" v-model="queryParam.confirmorId"></a-input>-->
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="创建时间">
                <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.createTime_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.createTime_end"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('hf_pm_project_change_records')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length
        }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot='taskDes' slot-scope='text'>
          <j-ellipsis :value='text' :length='10' />
        </template>
        <template slot="changeCode" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ text }}</a>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
<!--              <a-menu-item>-->
              <!--                <a @click="handleDetail(record)">详情</a>-->
              <!--              </a-menu-item>-->
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a style="color:red">删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a @click="determineButton(record)">确认</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <hf-pm-project-change-records-modal ref="modalForm" @ok="modalFormOk"></hf-pm-project-change-records-modal>
    <hf-pm-project-subjects-select-modal ref="hfPmProjectSubjectsSelectModal" :multiple="false"
                                         @hf-pm-project-subjects="hfPmProjectsubjects"/>
    <pm-project-change-records-modal-detail
      ref="pmProjectChangeRecordsModalDetail"></pm-project-change-records-modal-detail>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import HfPmProjectChangeRecordsModal from './modules/HfPmProjectChangeRecordsModal'
import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
import {getAction} from '../../../api/manage'
import HfPmProjectSubjectsSelectModal from './modules/HfPmProjectSubjectsSelectModal'
import PmProjectChangeRecordsModalDetail from '@views/hifar/pm/modules/PmProjectChangeRecordsModalDetail'

export default {
  name: 'HfPmProjectChangeRecordsList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    HfPmProjectChangeRecordsModal,
    HfPmProjectSubjectsSelectModal,
    PmProjectChangeRecordsModalDetail
  },
  data() {
    return {
      description: 'hf_pm_project_change_records管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '变更编号',
          align: 'center',
          dataIndex: 'changeCode',
          scopedSlots: {customRender: 'changeCode'}
        },
        {
          title: '专题名称',
          align: 'center',
          dataIndex: 'subjectId_dictText',
          scopedSlots: { customRender: 'taskDes' }
        },
        {
          title: '项目简称',
          align: 'center',
          dataIndex: 'projectId_dictText',
          scopedSlots: { customRender: 'taskDes' }
        },
        {
          title: '变更类型',
          align: 'center',
          dataIndex: 'changeType_dictText'
        },
        {
          title: '变更描述',
          align: 'center',
          dataIndex: 'changeDes',
          scopedSlots: { customRender: 'taskDes' }
        },
        {
          title: '项目延期/天',
          align: 'center',
          dataIndex: 'projectDelay'
        },
        {
          title: '专题延期/天',
          align: 'center',
          dataIndex: 'subjectDelay'
        },
        {
          title: '人天成本',
          align: 'center',
          dataIndex: 'countPrice'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'changeState_dictText'
        },
        {
          title: '变更人',
          align: 'center',
          dataIndex: 'costWorkday_dictText'
        },
        {
          title: '变更时间',
          align: 'center',
          dataIndex: 'changeTime',
          customRender: function (text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '确认人',
          align: 'center',
          dataIndex: 'confirmorId_dictText'
        },
        {
          title: '确认时间',
          align: 'center',
          dataIndex: 'confirmorTime',
          customRender: function (text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: '/pm/hfPmProjectChangeRecords/list',
        delete: '/pm/hfPmProjectChangeRecords/delete',
        deleteBatch: '/pm/hfPmProjectChangeRecords/deleteBatch',
        exportXlsUrl: '/pm/hfPmProjectChangeRecords/exportXls',
        importExcelUrl: 'pm/hfPmProjectChangeRecords/importExcel',
        ascertain: 'pm/hfPmProjectChangeRecords/ascertain'
      },
      disableMixinCreated: true,
      queryModel:true
    }
  },
  created() {
  },
  props: {
    selectedLeftTreeData: {
      type: Object,
      default: () => {
      },
    },
  },
  watch: {
    selectedLeftTreeData: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val.selectedTreeType === 'project') {
          this.getListBySelectedKey(val.selectedKeys)
        } else if (val.selectedTreeType === 'subject') {
          this.getListBySelectedKey(val.selectedSubKeys)
        }
      },
    },
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleAdd() {
      this.$refs.modalForm.add();
      this.$refs.modalForm.title = "新增";
      this.$refs.modalForm.disableSubmit = false;
      this.$refs.modalForm.defaultModel = this.selectedLeftTreeData
    },
    getListBySelectedKey(id) {
      if (this.selectedLeftTreeData.selectedTreeType === 'project') {
        this.propsParams = {projectId: id}
        this.loadData()
      } else if (this.selectedLeftTreeData.selectedTreeType === 'subject') {
        this.propsParams = {subjectId: id}
        this.loadData()
      }
    },
    loadData() {
      let params = Object.assign({}, this.getQueryParams(), this.propsParams)
      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total;
        }
      }).finally(() => {
        this.loading = false;
      })
    },
    hfPmProjectsubjects(data) {
      this.$set(this.queryParam, 'subjectId', data.id)
      this.$set(this.queryParam, 'subjectName', data.subjectName)
    },
    subjectIdClick() {
      this.$nextTick(() => {
        this.$refs.hfPmProjectSubjectsSelectModal.title = '选择专题'
        this.$refs.hfPmProjectSubjectsSelectModal.visible = true
      })
    },
    determineButton(record) {
      record.confirmorId = this.$ls.get('Login_Userinfo').username
      record.confirmorTime = new Date().toLocaleDateString().replaceAll('/', '-')
      record.confirmorDes = '已确认'
      if (!this.url.ascertain) {
        this.$message.error('请设置url.ascertain属性!')
      }
      getAction(this.url.ascertain, record).then((res) => {
        if (res.success) {
          this.loadData()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleDetail: function (record) {
      record.clickArchive = false //为了识别是点击了编辑或详情
      this.$refs.pmProjectChangeRecordsModalDetail.edit(record)
      this.$refs.pmProjectChangeRecordsModalDetail.title = '详情'
      this.$refs.pmProjectChangeRecordsModalDetail.disableSubmit = true
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>